for knd = 1:10
xxx(knd)=find(proj_meta(3).rd(knd).velM(1,:)>0.005,1)
end

for knd= 1:2
    aa=proj_meta(3).rd(knd).act;
    aa(aa<1.5)=1;
    first_act=[];
    for ind=1:size(aa,1)
        tmp=find(aa(ind,xxx(knd):40000)>1,1,'first');
        if ~isempty(tmp)
        first_act(ind)=tmp; 
        else
        first_act(ind)=size(aa,2);
    end
    end
    [~,sort_id(knd,:)]=sort(first_act);
    figure('Position', [1 300 1600 550]), imagesc(aa(sort_id(knd,:),:))
    figure('Position', [1 1 1600 250]), plot(proj_meta(3).rd(knd).velM_smoothed)
end

aaa=ROIs2image(ROIs(yyy(1,:)), size(template), 'display',1, 'randcolor', 0, 'template', template);